<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{
     //评论管理   评论列表
    public function getIndex(Request $request){
        
        //分页
        $page = $request->input('page',1);
        //商品个数
        $num = $request->input('num',1);
        //获取gid
        if($request->input('id')==null){
            $gid = DB::table('shop_comment')->lists('gid');
            // 移除数组中重复的值
            $gid = array_unique($gid);
        }else{
            $gid[] = $request->input('id');
        }
        //搜索查询
        $likes = $request->input('likes');
        if($likes){
            // dd($gid);
            $like = DB::table('shop_goods')->where('gname','like','%'.$likes.'%')->lists('gid');
            // 返回一个数组，该数组包含了所有在 $gid 中也同时出现在所有其它参数数组中的值 交集
            $gid = array_intersect($gid,$like);
            // dd($gid);
        }
        //最大页数
        $count = ceil(count($gid)/$num);
        //上一页
        $prev = $page-1;
        if($prev<=1){$prev=1;}
        if($prev>=$count){$prev=$count;}
        //下一页
        $next = $page+1;
        if($next<=1){$next=1;}
        if($next>=$count){$next=$count;}
        // 将一个数组分割成多个数组，其中每个数组的单元数目由 size 决定
        $res = array_chunk($gid,$num,TRUE);
        // dd($res);
        $gid = $res[$page-1];
        // dd($gid);
        //全部评论数据
        $comment = DB::table('shop_comment')
            ->join('shop_users', 'shop_comment.uid', '=', 'shop_users.uid')
            ->join('shop_goods', 'shop_comment.gid', '=', 'shop_goods.gid')
            ->select('shop_comment.*', 'shop_users.*', 'shop_goods.*')
            ->get();

        $comments = null;
        //获取数据
        foreach ($comment as $k=> $v){
            foreach($gid as $kk=>$vv){
                if($v->gid==$vv){
                    $comments[$vv][] = $v;
                }
            }
        }
        // dd($comment);
        //商品评论为空  返回
        if($comments == null){
            //获取gname
            $gname = DB::table("shop_goods")->where('gid',$gid)->value('gname');
            return back()->with('error',$gname.'的商品评论为空');
        }
        // 获取参数列表
        $list = $request->all();
        // dd($comments);
        $zstus = ['0'=>'偏大','1'=>'正常','偏小'];
        return view('admin.goods.comment',['likes'=>$likes,'prev'=>$prev,'next'=>$next,'page'=>$page,'count'=>$count,'num'=>$num,'comments'=>$comments,'zstus'=>$zstus]);
    }
    
    //回复评论
    public function postReply(Request $request)
    {
        // dd($request->all());
        $zid = $request->input('zid');
        $data['zreply'] = $request->input('reply');
        //添加至数据库
        $res = DB::table('shop_comment')->where('zid',$zid)->update($data);
        echo $res;
    }
    //删除评论
    public function getDelpl(Request $request)
    {
        $zid = $request->input('zid');
        //删除
        $res = DB::table('shop_comment')->where('zid',$zid)->delete();
        echo $res;
    }
}
    
    